/*The MIT License

Copyright (c) <1/27/2011> <erick.difiore>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.*/
package com.d4a5.mapping.core
{
	public class MinMax
	{
		protected var _min:Number;
		protected var _max:Number;
		protected var _width:Number;
		protected var _changed:Boolean = false;
		protected var _valid:Boolean = false;
		protected var _spatialIndex:uint = 0;
		
		public function MinMax( min:Number , max:Number  )
		{
			if( min <= max ){
				this._min = min;
				this._max = max;
			}
			else{
				this._min = max;
				this._max = min;
			}
			
			
			this.invalidate();
		}
		
		public function get min() : Number {
			return this._min;
		}
		public function set min( num:Number ) : void {
			this._min = num;
			this.invalidate();
		}
		public function get max() : Number {
			return this._max;
		}
		public function set max( num:Number ) : void {
			this._max = num;
			this.invalidate();
		}
		
		
		protected function invalidate() : void {
			this._valid = ( isNaN( this._min ) == false && isNaN( this._max ) == false );
			
			if( this._valid ){
				if( this._min > this._max ){
					var temp:Number = this._max;
					this._max = this._min;
					this._min = temp;
				}
				this._width = this._max - this._min;
				
				this._spatialIndex = Utils.createMinMaxSpatialIndex( this._min , this._max  );
			}
		}
		
		public function get isValid() : Boolean {
			return this._valid;
		}
		
		
		public function inRange( num:Number ) : Boolean {
			return ( this._valid && num >= this._min && num <= this._max );
		}
		
		public function union( m:MinMax ) : void {
			if( m.isValid ){
				if( this._valid ){
					this._min = ( m.min < this._min ) ? m.min : this._min;
					this._max = ( m.max > this._max ) ? m.max : this._max;
					this.invalidate();
				}
				else{
					this._min = m.min;
					this._max = m.max;
					this.invalidate();
				}
			}
		}
		public function unionPt( num:Number ) : void {
			if( this._valid ){
				this._min = ( num < this._min ) ? num : this._min;
				this._max = ( num > this._max ) ? num : this._max;
				this.invalidate();
			}
			else{
				this._min = num;
				this._max = num;
				this.invalidate();
			}
		}
		
		public function get width() : Number {
			return this._width;
		}
		
		public function toString() : String {
			return "MIN(" + this._min.toString() + "),MAX(" + this._max.toString() + ")";;
		}
		
		public function get spatialIndex() :uint {
			return this._spatialIndex;
		}
		
	}
}